class Bare1Category < ActiveRecord::Base
  include BareMigration
end

class Bare1Article < ActiveRecord::Base
  include BareMigration
end

class Bare1ArticlesCategory < ActiveRecord::Base
  include BareMigration
end

class InitialSchema < ActiveRecord::Migration
  def self.up        
    ActiveRecord::Base.transaction do
      create_table :users do |t|
        t.column :login, :string
        t.column :password, :string
      end

      create_table :articles do |t|
        t.column :title, :string
        t.column :author, :string
        t.column :body, :text
        t.column :body_html, :text
        t.column :excerpt, :text
        t.column :keywords, :string
        t.column :allow_comments, :integer
        t.column :allow_pings, :integer
        t.column :published, :integer, :default => 1
        t.column :text_filter, :string
        t.column :created_at, :datetime
        t.column :updated_at, :datetime
        t.column :permalink, :string
      end

      add_index :articles, :permalink

      create_table :categories do |t|
        t.column :name, :string
        t.column :position, :integer
      end

      create_table :articles_categories, :id => false do |t|
        t.column :article_id, :integer
        t.column :category_id, :integer
        t.column :is_primary, :integer
      end

      category = Bare1Category.create(:name=>'default', :position=>'1')
      article = Bare1Article.create(:title=>'Hi,你好!',
        :author=>'Admin',
        :body=>'Welcome to ok2v. This is your first article. Edit or delete it, then start blogging!',
        :allow_comments => 1,
        :allow_pings =>1,
        :published => 1,
        :permalink => 'hello-world'
      )
      Bare1ArticlesCategory.create(:article_id=>article.id, :category_id=>category.id, :is_primary=>1)      
      
      create_table :comments do |t|
        t.column :article_id, :integer
        t.column :title, :string
        t.column :author, :string
        t.column :email, :string
        t.column :url, :string
        t.column :ip, :string
        t.column :body, :text
        t.column :body_html, :text
        t.column :created_at, :datetime
        t.column :updated_at, :datetime
      end

      add_index :comments, :article_id
      
      create_table :sessions do |t|
        t.column :sessid, :string
        t.column :data, :text
        t.column :created_at, :datetime
        t.column :updated_at, :datetime
      end

      create_table :settings do |t|
        t.column :name, :string
        t.column :value, :string
        t.column :position, :integer
      end
    
    end
  end

  def self.down
    ActiveRecord::Base.transaction do
      remove_index :articles, :permalink      
      remove_index :comments, :article_id
      remove_index :pings, :article_id
      drop_table :users
      drop_table :articles
      drop_table :categories
      drop_table :articles_categories      
      drop_table :comments   
      drop_table :sessions
      drop_table :settings
    end
  end
end
